// components/memberRecharge/memberRecharge.js
let ani1 = null;
let ani2 = null;
const api = require('../../utils/interface.js')
const math = require('../../utils/math.js')
Component({
  /**
   * 组件的属性列表
   */
  pageLifetimes: {
    show: function() {
      // 页面被展示
      getApp().login().then(res => {
        this.getStore()
        this.getMemberCard()
      })
    },
  },
  properties: {
    userInfo: null
  },
  observers:{
    'userInfo':function(e){
      this.getMemberCard()
    }
  },
  data: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
    animation1: null,
    animation2: null,
    state: 1,
    memberCardInfo: {},
    memberShowInfo: {
      orderAmount: 0,
      rechargeBalance: 0,
      redGiftAmount: 0,
      memberBalance:0
    },
    stationDistance: {},
    lat:0,
    lot:0
  },
  created() {
    ani1 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
    ani2 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
    getApp().login().then(res => {
      this.getStore()
      this.getMemberCard()
    })
   
  },
  lifetimes: {
    attached: function() {
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 管理副卡
    go_subCard() {
      if (!this.data.userInfo.phone) {
        wx.redirectTo({
          url: '/pages/login/login',
        })
        return false
      }
      console.log(this.data.userInfo)
      wx.navigateTo({
        url: '/pages/webView/webview?url=' + getApp().globalData.url + 'SubCardBox&cardId=' + this.data.memberCardInfo.id + '&phone=' + this.data.userInfo.phone,
      })
    },
    // 会员卡详情
    go_memberCard() {
      wx.navigateTo({
        url: '/pages/memberCard/memberCard?uuid=' + this.data.memberCardInfo.uuid + '&cardId=' + this.data.memberCardInfo.id + '&productId=' + this.data.memberCardInfo.productId + '&name=' + this.data.memberCardInfo.productName+'&index=0',
      })
    },
    // 会员卡账单
    go_order() {
      wx.navigateTo({
        url: '/pages/orderList/order?cardId=' + this.data.memberCardInfo.id,
      })
    },
    start() {
      if (this.data.state == 1) {
        ani1.height(0).step()
        ani2.rotate(180).step()
        this.data.state = 2
      } else {
        ani2.rotate(0).step()
        ani1.height('206rpx').step()
        this.data.state = 1
      }
      this.setData({
        animation1: ani1.export(),
        animation2: ani2.export()
      })
    },
    // 充值
    go_racharge() {
      wx.navigateTo({
        url: '/pages/memberReacarge/memberRecharge?memberCardId=' + this.data.cardId + '&stationId=' + this.data.memberCardInfo.storeId + '&parductId=' + this.data.memberCardInfo.productId,
      })
    },
    go_cardPackage() {
      wx.navigateTo({
        url: '/pages/cardPackage/cardPackage',
      })
    },
    // 获取当前使用会员卡
    getMemberCard() {
      
      let cardInfo = this.data.userInfo.firstMemberInfoCardDTO
      this.data.memberShowInfo.rechargeBalance = math.divide(cardInfo.rechargeBalance, 100)
      this.data.memberShowInfo.redGiftAmount = math.divide(cardInfo.redGiftAmount, 100)
      this.data.memberShowInfo.memberBalance = math.plus(this.data.memberShowInfo.rechargeBalance, this.data.memberShowInfo.redGiftAmount)
      this.data.cardId = cardInfo.id
      this.setData({
        memberShowInfo: this.data.memberShowInfo,
        memberCardInfo: cardInfo
      })
      console.log(this.data.memberCardInfo,"更新卡信息")
      wx.hideLoading()
    },
    gocoupon(){
      wx.navigateTo({
        url: '/pages/couponcard/couponcard?membercardId=' + this.data.cardId
      })
    },
    // 吊起地图
    go_map() {
      getApp().copyCityId()
      wx.openLocation({
        latitude: Number(this.data.stationDistance.lat),
        longitude: Number(this.data.stationDistance.lot),
        scale: 18
      })
    },
    // 推荐站点--油站详情
    goStationInfo() {
      console.log(this.data)
      wx.navigateTo({
        url: '/pages/webView/webview?url=' + getApp().globalData.url + 'stationInfo&stationId=' + this.data.stationDistance.id + '&lat=' + this.data.lat + '&lot=' + this.data.lot,
      })
    },
    // 获取推荐站点
    getStore(){
      let that= this
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          that.setData({
            lat: res.latitude,
            lot: res.longitude
          })
          api.getStoreOnMemberHome({
            lat: res.latitude,
            lot: res.longitude
          }).then(res => {
            res.data.result.distance = res.data.result.distance?(res.data.result.distance/1000).toFixed(2):0
            that.setData({
              stationDistance:res.data.result
            })
          })
        },
        
      })
    }
  }
})